package cn.janescott.inner.space.webapp.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Web访问日志
 * @author scott
 * @date 2018/8/9
 */
@Aspect
public class WebAccessLogAspect {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 切面(注解)
     */
    @Pointcut("@annotation(annotation)")
    private void access(WebAccessLogAnnotation annotation) {}

    @Before("access(annotation)")
    public void doBeforeAdvice(JoinPoint joinPoint, WebAccessLogAnnotation annotation) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        logger.info(request.getRemoteAddr());
        logger.info(request.getRequestURI());
        MethodSignature signature = (MethodSignature)(joinPoint.getSignature());
        logger.info(signature.getName());
        logger.info(signature.getMethod().getName());
    }
}
